//
//  NSString+Date.m
//  MapDemo
//
//  Created by Fidetro on 16/5/24.
//  Copyright © 2016年 Fidetro. All rights reserved.
//

#import "NSString+FIDDate.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

@implementation NSString (FIDDate)


+ (NSString *)getYearDateWithDaySince1970:(NSTimeInterval)time dateFormat:(NSString *)dateFormat{
    NSDate *date =[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    formatter.dateFormat = dateFormat;
    NSString *dateString = [formatter stringFromDate:date];
    return dateString;
}
+ (NSString *)getYearToDay{
 
    return [NSString getYearDay:0];
}

+ (NSString *)getYearLastDay{

    return [NSString getYearDay:-1];
}
+ (NSString *)getYearBeforeDay{

    return [NSString getYearDay:-2];
}
+ (NSString *)getYearDay:(NSInteger)dayFromToday{
    NSString *today =  [NSString getDaySince1970:dayFromToday];
    NSDate *date =    [NSDate dateWithTimeIntervalSince1970:[today integerValue]];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy年MM月dd日";
    NSString *dayString = [formatter stringFromDate:date];
    return dayString;
}
+ (NSString *)getTodaySince1970{

    
    return [NSString getDaySince1970:0];
    
}
+ (NSString *)getCurrentSince1970{
    NSDate *date = [NSDate date];
    
    
    NSString *timeInterval = [NSString stringWithFormat:@"%f", date.timeIntervalSince1970];
    NSString *dateTime = [[timeInterval componentsSeparatedByString:@"."] objectAtIndex:0];
    
    return dateTime;
    
}
+ (NSString *)getLastDaySince1970{

       
    return [NSString getDaySince1970:-1];
}
+ (NSString *)getDaySince1970:(NSInteger)dayFromToday{
    NSTimeInterval time = 24 * 60 * 60 * dayFromToday;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSDate *date = [NSDate date];
    
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    formatter.dateFormat = @"yyyy-MM-dd 00:00 ";
    NSString *dateString = [formatter stringFromDate:date];
    
    NSDate *lastDate=[formatter dateFromString:dateString];
    NSString *timeInterval = [NSString stringWithFormat:@"%f", lastDate.timeIntervalSince1970+time];
    NSString *dateTime = [[timeInterval componentsSeparatedByString:@"."] objectAtIndex:0];
    
    
    return dateTime;
}
+(NSString *)getMMSSFromSS:(NSInteger)seconds{
    
    
    
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    
    return format_time;
    
}
+ (NSString *)getBeforeYesterdaySince1970{
   
    
    
    return [NSString getDaySince1970:-2];
}
+ (NSString *)getWeek{
    NSArray * arrWeek=[NSArray arrayWithObjects:@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六", nil];
    NSDate *date = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [calendar setTimeZone:[NSTimeZone localTimeZone]];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    
    components = [calendar components:NSCalendarUnitWeekday fromDate:date];
    NSInteger week = [components weekday];
    return arrWeek[week-1];
}



+ (NSString *) compareCurrentTime:(NSString *)str
{
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:timeDate];
    long temp = 0;
    NSString *result;
    if (timeInterval/60 < 1)
    {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    return  result;
}

+ (NSString *)compareToMin:(NSString *)Min{
    NSString *result = nil;
    NSInteger  min = [Min integerValue];
    if (min < 60) {
        result = [NSString stringWithFormat:@"%ldmin",min];
    }else if (min < 24 * 60){
        result = [NSString stringWithFormat:@"%ldh",min/60];
    }else if (min < 30 * 24 * 60){
        result = [NSString stringWithFormat:@"%ld天",min/(24 * 60)];
    }else{
        result = @"1月+";
    }
    return result;
}
+ (NSString *)compareToSecond:(NSString *)second{
    NSString *result = nil;
    NSInteger  sec = [second integerValue];
    sec = [NSDate date].timeIntervalSince1970 - sec ;
    sec /= 60;
    if (sec < 60) {
        result = [NSString stringWithFormat:@"%ldmin",sec];
    }else if (sec < 24 * 60){
        result = [NSString stringWithFormat:@"%ldh",sec/60];
    }else if (sec < 30 * 24 * 60){
        result = [NSString stringWithFormat:@"%ld天",sec/(24 * 60)];
    }else{
        result = @"1月+";
    }
    return result;
}

#pragma clang diagnostic pop
@end
